package com.ctsi.system.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctsi.common.util.Search;
import com.ctsi.core.atomsms.core.AtomSmsTemplate;
import com.ctsi.core.atomsms.entity.SmsMsg;
import com.ctsi.core.common.util.StringUtil;
import com.ctsi.core.database.util.PageUtil;
import com.ctsi.system.entity.SmsSysMsg;
import com.ctsi.system.mapper.SmsMsgMapper;
import com.ctsi.system.service.ISmsMsgService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 短信信息模块 服务实现类
 * </p>
 *
 * @author fxd
 * @date 2022-03-15 16:30:51
 */
@Service
@RequiredArgsConstructor
public class SmsMsgServiceImpl extends ServiceImpl<SmsMsgMapper, SmsSysMsg> implements ISmsMsgService {
	private final AtomSmsTemplate smsTemplate;

	@Override
	public IPage<SmsSysMsg> listPage(Search search) {
		LambdaQueryWrapper<SmsSysMsg> queryWrapper = Wrappers.lambdaQuery();
		if (StringUtil.isNotBlank(search.getKeyword())) {
			queryWrapper.like(SmsSysMsg::getId, search.getKeyword());
		}
		queryWrapper.orderByDesc(SmsSysMsg::getCreateTime);
		return this.baseMapper.selectPage(PageUtil.getPage(search), queryWrapper);
	}

	@Override
	public boolean send(SmsSysMsg smsSysMsg) {
		SmsMsg smsMsg = new SmsMsg();
		smsMsg.setSmsTemplateSign(smsSysMsg.getTplSign());
		smsMsg.setSmsSignatureSign(smsSysMsg.getSignSign());
		smsMsg.setMobile(smsSysMsg.getMobile());
		smsMsg.setParams(JSON.parseObject(smsSysMsg.getParams()));
		smsTemplate.sendMsg(smsMsg);
		return Boolean.TRUE;
	}
}
